package cn.itcast.day02.duotai05;
/*
   向上转型:父类引用指向子类对象  (默认)

   弊端:不能调用子类特有的

   向下转型(可以调用子类特有的)
     double b = 10;
     int i = (int)b

     Animal animal = new Dog()
     Dog dog = (Dog) animal

     Cat cat = (Cat)animal--->将代表狗的animal转成了cat-->ClassCastException(类型转换异常)


    关键字:
       instanceof

       用于判断


       对象名 instanceof 对象-->前面的是否属于后面的对象


 */
public class Test {
    public static void main(String[] args) {

       Animal animal = new Dog();
        animal.eat();

        //向下转型
        Dog dog1 = (Dog)animal;
        dog1.lookDoor();

         /*Cat cat = (Cat)animal;
        cat.catchMouse();*/

        Dog dog = new Dog();
        method(dog);

        Cat cat = new Cat();
        method(cat);
    }

    public static void method(Animal animal){
        if (animal instanceof Dog){
            animal.eat();
            Dog dog = (Dog)animal;
            dog.lookDoor();
        }

        if (animal instanceof Cat){
            animal.eat();
            Cat cat = (Cat)animal;
            cat.catchMouse();
        }

    }
}
